//
//  MainViewController.m
//  News_lzz
//
//  Created by qianfeng01 on 15-6-14.
//  Copyright (c) 2015年 李忠. All rights reserved.
//

#import "MainViewController.h"


@interface MainViewController ()
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation MainViewController

- (instancetype)init {
    if (self = [super init]) {
        self.typeArr = [[NSArray alloc] init];
        self.manager = [AFHTTPRequestOperationManager manager];
        self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        self.dataArr = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self firstDo];
    [self createTableView];
    
}

- (void)firstDo {
//对tableView等进行一些初始化
}

#pragma mark - 下载并解析
- (void)requestNetAndDownloadDataWithUrlType:(NSString *)urlType {
    NSString *url = [NSString stringWithFormat:kUrl,urlType];
    __weak typeof(self) weakSelf = self;
    [self.manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSLog(@"下载成功");
            [weakSelf parserWithData:responseObject];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败,error: %@",error);
    }];
}

- (void)parserWithData:(id)responseObject {
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
    NSDictionary *dataDict = dict[@"data"];
    NSArray *listArr = dataDict[@"list"];
    [self.dataArr removeAllObjects];
    for (NSDictionary *modelDict in listArr) {
        Model *model = [[Model alloc] init];
        [model setValuesForKeysWithDictionary:modelDict];
        if (model.is_focus) {
            [self reloadHeadView:model];
            continue;
        }
        [self.dataArr addObject:model];
    }
    [self.tableView reloadData];
}

- (void)reloadHeadView:(Model *)model {
    [self.adView loadImage:model.kpic];
    self.tableView.tableHeaderView = self.adView;
}

#pragma mark - UITableView 和 协议方法
/****************************************************/
- (void)createTableView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenSize.width, kScreenSize.height-64-49) style:UITableViewStylePlain];
    /************************/
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%ld",self.dataArr.count);
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
    NewsCell *newsCell = [tableView dequeueReusableCellWithIdentifier:@"NewsCellOne" forIndexPath:indexPath];
    return [newsCell setCellWithModel:self.dataArr[indexPath.row]];
     */
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Model *model = self.dataArr[indexPath.row];
    WebViewController *web = [[WebViewController alloc] init];
    web.url = model.link;
    [self.navigationController pushViewController:web animated:YES];
}

/******************************************/
- (void)itemClick:(UIBarButtonItem *)item {
}
/******************************************/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
 */


@end
